<template>
  <div class="app-wrapper">
    <div class="main" :style="{height: wHeight - 120 + 'px'}">
      <div class="register-box">
        <h3>企业信息注册激活</h3>
        <el-form :model="registerForm" status-icon ref="registerForm" class="login-form">
          <el-form-item prop="bid" label="请输入企业海关编码激活系统">
            <el-input v-model="registerForm.bid" placeholder="请输入企业海关编码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')" :loading="loading" style="width:100%">下一步</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { getCompany,getCompanyInfo } from '@/api/register';

export default {
  data() {
    return {
      wHeight: 0,
      registerForm: {
        bid: ''
      },
      rules: {
        bid: [
          {required: true, message: '请输入企业海关编码ID', trigger: 'blur'},
        ]
      },
      loading: false
    };
  },
  created() {},
  mounted() {
    this.setHeight();
    window.onresize = () => {
      this.setHeight();
    };
  },
  methods: {
    setHeight() {
      let oHtml = document.getElementsByTagName('html')[0];
      let wHeight = oHtml.clientHeight;
      this.wHeight = wHeight;
    },
    submitForm() {
      let that = this;
      var bFlag = true;
      if (!this.registerForm.bid || !this.registerForm.bid == '') {
          if(this.registerForm.bid.length<8){

              that.$notify.error({
                  title: '错误',
                  message: '请输入正确的企业海关编码'
              });
              bFlag = false;
              return;
          }

      }
      if (bFlag == true) {
        that.loading = true;
        var tokenL = '';
        if (localStorage.hasOwnProperty('token')) {
          tokenL = localStorage.getItem('token');
        }
        getCompany(tokenL, that.registerForm.bid)
          .then(data => {
            that.loading = false;
            var myData = data.data;
            if (myData.length <= 0) {
              //没有查到对应的企业信息
              that.registerForm.bid = '';

              that.$notify.error({
                  title: '错误',
                  message: '请输入正确的企业海关编码'
              });
            } else {
                getCompanyInfo(that.registerForm.bid).then(res=>{
                    if(res.code =='0'){
                        var myData = res.data;
                        myData = JSON.stringify(myData[0]); //转化为JSON字符串
                        localStorage.setItem('CompanyInfo', myData);
                        that.$router.push({ path: '/register/step2' });
                    }else{
                        //...
                    }
                }).catch(err=>{
                    console.log(err)
                })

            }
          })
          .catch(function(error) {
            console.log(error);
            that.loading = false;
          });
      }
    }
  }
};
</script>
<style rel='stylesheet/scss' lang="scss" scoped>
@import 'src/styles/mixin.scss';
.app-wrapper {
  @include clearfix;
  width: 100%;
}

.main {
  position: relative;
  background: #0c75c1;
  padding-top: 160px;
}

.register-box {
  background: #fff;
  width: 500px;
  margin: auto;
  padding: 20px 30px;
  border-radius: 6px;
  h3 {
    text-align: center;
  }
}
</style>
